package cn.wzvtc.soft14;

/**
 * Created by asus on 2016/3/22.
 */
        import java.sql.DriverManager;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        import java.util.ArrayList;
        import java.util.List;

        import com.mysql.jdbc.Connection;
        import com.mysql.jdbc.PreparedStatement;

public class OldJdbcImpl implements OldJdbc{

        String driver = "com.mysql.jdbc.Driver";
        String userName = "soft14025220";
        String password = "soft14025220";
        String url = "jdbc:mysql://56ea58214b633.sh.cdb.myqcloud.com:6544/soft14025220?characterEncoding=utf-8";

       /* String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "lj6011600";
        String url="jdbc:mysql://localhost:3306/exam?characterEncoding=utf-8";*/

    public void insertPerson(String id,String name,String pass) {
        try {
            Class.forName(driver);
            Connection conn = (Connection) DriverManager.getConnection(url, userName,password);
            PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement("insert into student_info (id,studentid,studentpass) values (?,?,?)");
            pstmt.setString(1, id);
            pstmt.setString(2, name);
            pstmt.setString(3, pass);
            pstmt.executeUpdate();

            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public List findAllPerson() {
        ArrayList list = new ArrayList();
        try {
            Class.forName(driver);
            Connection conn = (Connection) DriverManager.getConnection(url, userName,password);
            PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement("select * from student_info");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                student_info si=new student_info();
                si.setStudentid(rs.getString(2));
                si.setStudentpass(rs.getString(3));
                list.add(si);
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
